package com.randy.fundation;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f'};

    public static String md5(String string) {
        byte[] hash;
        try {
            hash = string.getBytes("UTF-8");
            return md5(hash);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        return "unknown";
    }

    public static String md5(byte[] bytes) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    public static final String SPREAD_SECRET = "video7~8!5g@9e#6today$7%0^3*le_123app";

    public static String signByMD5(String unsigned) {
        // 增补上防反编译破解的最后一位
        return signByMD5(unsigned, SPREAD_SECRET + "7");
    }

    public static String signByMD5(String unsigned, String secret) {
        String cipher = md5(unsigned + secret);
        return cipher;
    }

    public static String fileMD50(String path) {
        MessageDigest digest;
        InputStream   is     = null;
        File          file;
        String        output = null;
        try {
            digest = MessageDigest.getInstance("MD5");

            file = new File(path);
            is = new FileInputStream(file);

            byte[] buffer = new byte[8192];
            int    read;

            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[]     md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            output = bigInt.toString(16);
            // Fill to 32 chars
            output = String.format("%32s", output).replace(' ', '0');

        } catch (Exception e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                Logger.e("Exception on closing MD5 input stream", e);
            }
        }
        return output;
    }

    public static String fileMD5(String filename) {
        File file = new File(filename);
        return fileMD5(file);
    }

    public static String fileMD5(File file) {
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            MessageDigest md5    = MessageDigest.getInstance("MD5");
            byte[]        buffer = new byte[1024];
            int           numRead;
            while ((numRead = fis.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead);
            }

            fis.close();
            fis = null;

            byte[]        hash = md5.digest();
            StringBuilder sb   = new StringBuilder(hash.length * 2);
            for (int i = 0; i < hash.length; i++) {
                sb.append(HEX_DIGITS[(hash[i] & 0xf0) >>> 4]);
                sb.append(HEX_DIGITS[hash[i] & 0x0f]);
            }
            return sb.toString();
        } catch (Exception e) {
            System.out.println("error");
            return "";
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                Logger.e("Exception on closing MD5 input stream", e);
            }
        }
    }
}
